package com.hzy.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hzy.pms.domain.bo.SkuBo;
import com.hzy.pms.domain.vo.SkuVo;
import com.hzy.pms.domain.Sku;
import com.hzy.pms.mapper.SkuMapper;
import com.hzy.pms.service.ISkuService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * sku信息Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class SkuServiceImpl implements ISkuService {

    private final SkuMapper baseMapper;

    /**
     * 查询sku信息
     */
    @Override
    public SkuVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询sku信息列表
     */
    @Override
    public TableDataInfo<SkuVo> queryPageList(SkuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Sku> lqw = buildQueryWrapper(bo);
        Page<SkuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询sku信息列表
     */
    @Override
    public List<SkuVo> queryList(SkuBo bo) {
        LambdaQueryWrapper<Sku> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Sku> buildQueryWrapper(SkuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Sku> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSkuSnapshotId() != null, Sku::getSkuSnapshotId, bo.getSkuSnapshotId());
        lqw.eq(bo.getProductId() != null, Sku::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getOutSkuId()), Sku::getOutSkuId, bo.getOutSkuId());
        lqw.eq(bo.getPrice() != null, Sku::getPrice, bo.getPrice());
        lqw.eq(bo.getStock() != null, Sku::getStock, bo.getStock());
        lqw.eq(StringUtils.isNotBlank(bo.getSpData()), Sku::getSpData, bo.getSpData());
        return lqw;
    }

    /**
     * 新增sku信息
     */
    @Override
    public Boolean insertByBo(SkuBo bo) {
        Sku add = BeanUtil.toBean(bo, Sku.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改sku信息
     */
    @Override
    public Boolean updateByBo(SkuBo bo) {
        Sku update = BeanUtil.toBean(bo, Sku.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Sku entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除sku信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
